<?php
	
	class LogHandler{
			
		const FILE = "Log/log.txt";
		
		//Static arrays to store messages and errors
		public static $messageArray = array();
		public static $errorArray = array();
		
		/**
		 * Public static function to store messages
		 * @param String
		 * @return -
		 */
		public static function logMessage($message){
			LogHandler::$messageArray[] = $message;
		}
		
		/**
		 * Public static function to store Errors in array, will allso write to file
		 * @param String
		 * @return - 
		 */
		public static function logError($error){
			$error = date("Y-m-d H:i:s") . " | " .  $error . "\r\n";
			LogHandler::$errorArray[] = $error;
			LogHandler::writeErrorToFile($error);
		}
		
		/**
		 * Private function to write errormessages to file
		 * @param string errormessage
		 * @return -
		 */
		public static function writeErrorToFile($error){
			
			//Check that the file exist and that we can write to it
			if(is_writable(self::FILE)){
				
				//Try to open the file
				if(!$handle = fopen(self::FILE, 'a')){
					LogHandler::logError("Cannot open file: " . self::FILE);
					return;
				}
				
				//Try to write to the bottom of our file
				if(fwrite($handle, $error) === false){
					LogHandler::logError("Cannot write to file: " . self::FILE);
					return;
				}
				
				//Close file
				fclose($handle);
			}
			else{
				LogHandler::logError("File: " . self::FILE . " is not writeble");
				return;
			}
		}
		
		public static function readErrorsFromLogFile(){
			/**
			 * TODO Implement
			 */
		}
	}